package com.han.gift.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.han.gift.common.util.AjaxResult;
import com.han.gift.common.util.PageList;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.han.gift.dto.TenantDto;
import com.han.gift.entity.Tenant;
import com.han.gift.entity.Tenant;
import com.han.gift.entity.TenantType;
import com.han.gift.query.TenantQuery;
import com.han.gift.service.TenantService;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.util.Strings;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.io.Serializable;
import java.util.List;

@RestController
@RequestMapping("tenant")
public class TenantController {

    //@Autowired
    //public ${table.serviceName} tenantService;

    /**
     * 服务对象
     */
    @Autowired
    private TenantService tenantService;


    /**
     * 保存和修改公用的
     *
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Tenant tenant) {
        try {
            if (tenant.getId() != null)
                tenantService.updateById(tenant);
            else
                tenantService.save(tenant);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("保存对象失败！" + e.getMessage());
        }
    }

    /**
     * 删除对象信息
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {

            tenantService.removeById(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("删除对象失败！" + e.getMessage());
        }
    }

    //获取用户
    @GetMapping("/{id}")
    public AjaxResult get(@PathVariable("id") Long id) {
        try {

            Tenant tenant = tenantService.getById(id);
            return AjaxResult.me().setResultObj(tenant);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取一个失败！" + e.getMessage());
        }
    }


    /**
     * 查看所有的员工信息
     *
     * @return
     */
    @GetMapping
    public AjaxResult list() {

        try {
            List<Tenant> list = tenantService.list(null);
            return AjaxResult.me().setResultObj(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取所有失败！" + e.getMessage());
        }
    }


    /**
     * 分页查询数据
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */

    /**
     * 分页查询数据
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping("/list")
    public AjaxResult json(@RequestBody TenantQuery query) {
        Page<Tenant> page = new Page<>(query.getPage(), query.getRows());
        String keyword = query.getKeyword();
        PageList pageList = tenantService.getPageList(page, keyword);

//        if (Strings.isNotBlank(keyword)) {
//            LambdaQueryWrapper<Tenant> queryWrapper = new LambdaQueryWrapper<>();
//            queryWrapper.like(Tenant::getCompanyName, keyword)
//                    .or().like(Tenant::getAddress, keyword);
//
//            page = tenantService.page(page, queryWrapper);
//            PageList pageList = new PageList<Tenant>(page.getTotal(), page.getRecords());
//            return AjaxResult.me().setResultObj(pageList);
//        }
//        page = tenantService.page(page);
//        PageList pageList = new PageList<Tenant>(page.getTotal(), page.getRecords());
        return AjaxResult.me().setResultObj(pageList);
    }


    //店铺入驻
    @PostMapping("/register")
    public AjaxResult shopRegister(@RequestBody @Valid TenantDto dto) {
        try {
            return tenantService.shopRegister(dto);

        } catch (Exception e) {
            return AjaxResult.me().setSuccess(false).setMessage("操作失败"+e.getMessage());
        }
    }

}

